<template lang="html">
  <div class="container" v-loading="loading">
      <div class="search">
          <el-form :inline="true" class="demo-form-inline">
              <el-form-item>
                <el-input class="form_input" v-model="searchText" placeholder="请输入测量师姓名"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
              </el-form-item>
              <el-form-item>
                <datePicker startEndTime @onFilter="onFilter" />
              </el-form-item>
              <el-form-item>
                <span>统计时间：{{statisTime}}</span>
              </el-form-item>
          </el-form>
          <div class="export_excl">
            <el-button size="small" :disabled="isShutdownAudit" @click="exportExcl" type="primary">导出<i class="el-icon-upload el-icon--right"></i></el-button>
          </div>
      </div>

      <div class="tables">
          <el-table :data="tableData" @sort-change="sortChange" @selection-change="handleSelectionChange" :row-class-name="rowStyle" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
            <el-table-column type="selection" width="40"></el-table-column>
            <el-table-column prop="realName" label="姓名" align="center"></el-table-column>
            <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
            <el-table-column prop="paymentPrice" sortable label="已结款" align="center"></el-table-column>
            <el-table-column prop="noPaymentPrice" sortable label="未结款" align="center"></el-table-column>
            <el-table-column prop="dealCustomer" sortable label="成交客户" align="center"></el-table-column>
            <el-table-column prop="dealStaff" sortable label="成交员工" align="center"></el-table-column>
            <el-table-column prop="newMember" sortable label="新增客户" align="center"></el-table-column>
            <el-table-column prop="returnPrice" sortable label="返单金额" align="center"></el-table-column>
            <el-table-column prop="returnOrder" label="返单人数" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button size="mini" type="danger" @click="handleDetail(scope.row)">详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
      </div>

  </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      searchText: '',
      statisTime: '',
      year: '',
      month: '',
      day: '',
      date: [],
      sortMethod: null,
      sortColum: '',
      tableData: [],
      currentData: [],
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
    }
  },
  computed:{
    isShutdownAudit:function(){
        return this.currentData.length == 0?true:false;
    }
  },

  created(){
    this.getServeData();
  },
  methods: {
    getServeData(){
      this.loading = true;
      this.$http.selectDataStatsDesignerPage({
        query: this.searchText,
        startTime: (this.date&&this.date[0]) || '',
        endTime: (this.date&&this.date[1]) || '',
        dayTime: this.day,
        monthTime: this.month,
        yearTime: this.year,
        sortMethod: this.sortMethod,
        sortColum: this.sortColum,
        pageNumber: this.currentPage,
        pageSize: this.pageSize
      }).then(res=>{
        this.loading = false;
        this.tableData = res.data.rows;
        this.totalPage = res.data.total;
        this.pageSize = res.data.pageSize;
      });
    },
    sortChange({order,prop}){
      this.sortColum = prop;
      if(order == "descending") this.sortMethod = 2;
      else if(order == "ascending") this.sortMethod = 1;
      else this.sortMethod = null;
      this.getServeData();
    },
    exportsAPI(ids){
      ids && (
        this.loading = true,
        this.$http.dataStatsExportDesigner({
          ids,
          startTime: (this.date&&this.date[0]) || '',
          endTime: (this.date&&this.date[1]) || '',
          dayTime: this.day,
          monthTime: this.month,
          yearTime: this.year,
        }).then(res=>{
          this.loading = false,
          console.log(res)
        })
      );
    },
    onFilter(e){
      this.statisTime = e.val;
      this.date = [];
      this.year = '';
      this.month = '';
      this.day = '';
      switch (e.key) {
        case 1: this.day = e.val; break;
        case 2: this.month = e.val; break;
        case 3: this.year = e.val; break;
        case 4:
            this.date = [...e.val];
            if(e.val[0]) this.statisTime = `${e.val[0]} - ${e.val[1]}`;
            else this.statisTime = '';
        break;
      }
      this.onSubmit()
    },

    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    handleDetail(e){
      this.$router.push({ path: '/statistics/sales_statistics/user_detail', query: {id: e.id}});
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.getServeData();
    },
    exportExcl(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('确定要导出吗？').then(()=>{
        this.exportsAPI(aId.join(','));
      }).catch(e=>0);
    }
  }
}
</script>

<style lang="css" scoped>
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.active{
  color: #FFF;
  background-color: #409EFF;
  border-color: #409EFF;
}
.search{
  display: flex;
  position: relative;
}
.export_excl{
  position: absolute;
  right: 0
}
/deep/ .ascending .cell{
  color: #409EFF
}
/deep/ .descending .cell{
  color: #409EFF
}
</style>
